<template>
<div>
  <el-card>
    <!-- 顶部面包屑标识与导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right" style="font-size: 16px;">
      <el-breadcrumb-item :to="{ path: '/' }">
        <i class="el-icon-s-promotion"></i> 后台管理
      </el-breadcrumb-item>
      <el-breadcrumb-item>角色管理</el-breadcrumb-item>
    </el-breadcrumb>
    <el-divider></el-divider>
    <el-row  style="margin-top: 10px">
      <el-button type="primary" @click="openAddNewDialog()"  style="margin-left: 10px">新增</el-button>
      <download-excel
          style="float: left"
          class="export-excel-wrapper"
          :data="roleData"
          type="xls"
          worksheet="worksheet"
          name="用户信息">
        <el-button type="primary">导出</el-button>
      </download-excel>
    </el-row>
    <el-table
        :data="roleData"
        style="width: 100%">
      <el-table-column
          prop="id"
          label="角色编号"
          width="180">
      </el-table-column>
      <el-table-column
          prop="name"
          label="角色名称"
          width="180">
      </el-table-column>
      <el-table-column
          prop="state"
          label="角色状态">
      </el-table-column>
      <el-table-column
          prop="authority"
          label="权限">
        <template slot-scope="scpoe">
        <el-dropdown>
          <span class="el-dropdown-link">查看权限</span>
          <el-dropdown-menu slot="dropdown">
            <el-dropdown-item v-for="(value,key) in scpoe.row.authorityName" :key="key">{{value}}
            </el-dropdown-item>
            <el-dropdown-item v-if="scpoe.row.authorityName.length==0">当前无权限
            </el-dropdown-item>
          </el-dropdown-menu>
        </el-dropdown>
        </template>
      </el-table-column>
      <el-table-column
          prop="updateTime"
          label="更新时间">
      </el-table-column>
      <el-table-column label="操作"  width="200px"
                       align="center">
        <template slot-scope="scope">
          <el-button type="primary" icon="el-icon-edit" circle size="mini"
                     @click="openEditDialog(scope.row)"></el-button>
          <el-button type="danger" icon="el-icon-delete" circle size="mini"
                     @click="openDeleteConfirm(scope.row)"></el-button>
        </template>
      </el-table-column>
    </el-table>
  </el-card>
  <!-- 修改数据的表单 -->
  <el-dialog title="编辑用户管理数据" :visible.sync="editFormVisible">
    <el-form :model="editForm"  label-width="120px">
      <el-form-item label="用户编号" prop="roleId">
        <el-input v-model="editForm.id"></el-input>
      </el-form-item>
      <el-form-item label="角色名称" prop="name">
        <el-input v-model="editForm.name"></el-input>
      </el-form-item>
      <el-form-item label="角色状态" prop="state">
        <el-input v-model="editForm.state"></el-input>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="editFormVisible = false">取 消</el-button>
      <el-button type="primary" @click="handleEdit()">确 定</el-button>
    </div>
  </el-dialog>
  <!-- 新增数据的表单 -->
  <el-dialog title="新增角色数据" :visible.sync="FormVisible">
    <el-form :model="editForm"  label-width="120px">
      <el-form-item label="角色名称" prop="name">
        <el-input v-model="editForm.name"></el-input>
      </el-form-item>
      <el-form-item label="角色状态" prop="state">
        <el-input v-model="editForm.state"></el-input>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="FormVisible = false">取 消</el-button>
      <el-button type="primary" @click="addRole()">确 定</el-button>
    </div>
  </el-dialog>
</div>
</template>

<script>
export default {
  name: "role",
  data:function (){
    return {
      role:{},
      roleData:[],
      editFormVisible: false,
      FormVisible:false,
      editForm: {
        id: '',
        name: '',
        state: ''
      },
      addNewFormVisible:false,
    }
  },
  methods: {
    // 弹出修改对话框
    openEditDialog(tableItem) {
      let url = 'http://localhost:8080/v1/admins/' +tableItem.id;
      console.log('url = ' + url);
      this.axios
          .create({'headers': {'Authorization': localStorage.getItem('localJwt')}})
          .get(url).then((response) => {
        let jsonResult = response.data;
        console.log('jsonResult ='+ jsonResult)
        if (jsonResult.code == 1) {
          this.editForm = jsonResult.data;
          this.editFormVisible = true;
        } else {
          let title = '操作失败';
          this.$alert(jsonResult.message, title, {
            confirmButtonText: '确定',
            callback: action => {
            }
          });
        }
      });
    },
    // 执行修改
    handleEdit() {
      let url = 'http://localhost:8080/v1/admins/' + this.editForm.id + '/update';
      console.log('url = ' + url);
      let formData = this.qs.stringify(this.editForm);
      console.log('formData = ' + formData);

      this.axios
          .create({'headers': {'Authorization': localStorage.getItem('localJwt')}})
          .post(url, formData).then((response) => {
        let jsonResult = response.data;
        if (jsonResult.code ==1) {
          this.$message({
            message: '修改标签成功！',
            type: 'success'
          });
          this.editFormVisible = false;
          this.loadRoleList();
        } else if (jsonResult.state == 4040) {
          let title = '操作失败';
          this.$alert(jsonResult.message, title, {
            confirmButtonText: '确定',
            callback: action => {
              this.editFormVisible = false;
            }
          });
        } else {
          let title = '操作失败';
          this.$alert(jsonResult.message, title, {
            confirmButtonText: '确定',
            callback: action => {
            }
          });
        }
      });
    },
    // 打开新增对话框
    openAddNewDialog(tableItem) {
      this.FormVisible = true
    },
    // 执行新增
    addRole() {
      let url = 'http://localhost:8080/v1/admins/' + this.editForm.name + '/add-new';
      console.log('url = ' + url);
      let formData = this.qs.stringify(this.editForm);
      console.log('formData = ' + formData);
      this.axios
          .create({'headers': {'Authorization': localStorage.getItem('localJwt')}})
          .post(url, formData).then((response) => {
        let jsonResult = response.data;
        if (jsonResult.code ==1) {
          this.$message({
            message: '新增角色成功！',
            type: 'success'

          });
          this.FormVisible = false;
          this.loadRoleList();
        } else if (jsonResult.code== 4040) {
          let title = '操作失败';
          this.$alert(jsonResult.message, title, {
            confirmButtonText: '确定',
            callback: action => {
              this.FormVisible = false;
            }
          });
        } else {
          let title = '操作失败,新增的角色已存在!';
          this.$alert(jsonResult.message, title, {
            confirmButtonText: '确定',
            callback: action => {
            }
          });
        }
      });
    },

    // 打开删除确认框
    openDeleteConfirm(role) {
      let message = '此操作将永久删除【' + role.name + '】角色，是否继续？';
      this.$confirm(message, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.handleDelete(role);
      }).catch(() => {
      });
    },
    // 处理删除
    handleDelete(role) {
      let url = 'http://localhost:8080/v1/admins/' + role.id + '/delete';
      console.log('url = ' + url);

      this.axios
          .create({'headers': {'Authorization': localStorage.getItem('localJwt')}})
          .post(url).then((response) => {
        let jsonResult = response.data;
        if (jsonResult.code == 1) {
          this.$message({
            message: '删除【' + role.name + '】类别成功！',
            type: 'success'
          });
          this.loadRoleList();
        } else {
          this.$alert(jsonResult.message, '操作失败', {
            confirmButtonText: '确定',
            callback: action => {
            }
          });
        }
      })
    },
    roleHandleEdit(index,user){
      console.log(index);
    },
    roleHandleDelete(){
      console.log(index);
    },
    loadRoleList(){
      this.axios
          .create({'headers': {'Authorization': localStorage.getItem('localJwt')}})
          .get("http://localhost:8080/v1/admins/roleList").then((response)=>{
        if(response.data.code==1){
          this.roleData=response.data.data;
          console.log(this.roleData)
        }
      })
    },
  },
  mounted() {
    this.loadRoleList();
  }
}
</script>

<style scoped>
.el-dropdown-link {
  cursor: pointer;
  color: #409EFF;
}
.el-icon-arrow-down {
  font-size: 12px;
}
</style>